
#include <stdlib.h>
#include <stdio.h>

#if 0 /* aec + bf test eg. 4mic+1ref */
#include "kernels/dios_impl.h"
#define FRAMESIZE 128

int main(int argc, char *argv[])
{
    int M = 4;
    float pos[4][3] = {{0.03f,0.0f,0.0f},{0.0f,0.03f,0.0f},
                            {-0.03f,0.0f,0.0f},{0.0f,-0.03f,0.0f}};
    
    short *xdata = (short *)malloc((M+2)*FRAMESIZE*sizeof(short));
    short *xbuf = (short *)malloc(M*FRAMESIZE*sizeof(short));  
    short *refbuf = (short *)malloc(FRAMESIZE*sizeof(short));  
    short *ybuf = (short *)malloc(FRAMESIZE*sizeof(short));                 

    FILE *fidmic = fopen(argv[1], "rb");
    fseek(fidmic, 0, SEEK_END); int d2 = ftell(fidmic);
    fseek(fidmic, 0, SEEK_SET); int d1 = ftell(fidmic);
    int nframes = (d2-d1)/(FRAMESIZE*sizeof(short)*(M+2));
    printf("total frames = %d\n", nframes);
    FILE *fidaec = fopen(argv[2], "wb");    

    void *handle = dios_create_mics(M, pos);

    for (int n=0; n<nframes; n++) {
        fread(xdata, sizeof(short), (M+2)*FRAMESIZE, fidmic);
        for (int i=0; i<FRAMESIZE; i++) {
            for (int m=0; m<M; m++) {
                xbuf[FRAMESIZE*m+i] = xdata[(M+2)*i+m];
            }
            refbuf[i] = xdata[(M+2)*i+M];
        }      
        dios_process_frame(handle, xbuf, refbuf, ybuf);  
        fwrite(ybuf, sizeof(short), FRAMESIZE, fidaec);        
    }
    
    dios_destroy(handle);
    free(xdata); free(xbuf); free(refbuf); fclose(fidmic); fclose(fidaec);
    printf("beamforming test\n");
    return 0;
}
#endif


#if 1 /*multi channel bf test*/
#include "kernels/dios_impl.h"
#define FRAMESIZE 128
#define M 4
#define K 1

int main(int argc, char *argv[])
{
    float pos[4][3] = {{0.03f,0.0f,0.0f},{0.0f,0.03f,0.0f},
                            {-0.03f,0.0f,0.0f},{0.0f,-0.03f,0.0f}};
    
    short *xdata = (short *)malloc((M+K)*FRAMESIZE*sizeof(short));
    short *xbuf = (short *)malloc(M*FRAMESIZE*sizeof(short));    
    short *ybuf = (short *)malloc(FRAMESIZE*sizeof(short));                 

    FILE *fidmic = fopen(argv[1], "rb");
    fseek(fidmic, 0, SEEK_END); int d2 = ftell(fidmic);
    fseek(fidmic, 0, SEEK_SET); int d1 = ftell(fidmic);
    int nframes = (d2-d1)/(FRAMESIZE*sizeof(short)*(M+K));
    printf("total frames = %d\n", nframes);
    FILE *fidaec = fopen(argv[2], "wb");    

    void *handle = dios_create_mics(M, pos);

    for (int n=0; n<nframes; n++) {
        size_t size = fread(xdata, sizeof(short), (M+K)*FRAMESIZE, fidmic);
        if (size!=(M+K)*FRAMESIZE) break;
        for (int i=0; i<FRAMESIZE; i++) {
            for (int m=0; m<M; m++) {
                xbuf[FRAMESIZE*m+i] = xdata[(M+K)*i+m];
            }
        }      
        dios_process_frame(handle, xbuf, NULL, ybuf);  
        fwrite(ybuf, sizeof(short), FRAMESIZE, fidaec);        
    }
        
    free(xdata); free(xbuf); fclose(fidmic); fclose(fidaec);
    printf("beamforming test\n");
    return 0;
}
#endif

#if 0 /*single channel aec test*/
#include "kernels/dios_impl.h"
#define FRAMESIZE 128

int main(int argc, char *argv[])
{
    FILE *fidmic = fopen(argv[1], "rb");
    fseek(fidmic, 0, SEEK_END); int d2 = ftell(fidmic);
    fseek(fidmic, 0, SEEK_SET); int d1 = ftell(fidmic);
    int nframes = (d2-d1)/(FRAMESIZE*sizeof(short));
    printf("total frames = %d\n", nframes);

    FILE *fidref = fopen(argv[2], "rb");
    FILE *fidaec = fopen(argv[3], "wb");

    short micbuf[FRAMESIZE] = {0};
    short farbuf[FRAMESIZE] = {0};
    short aecbuf[FRAMESIZE] = {0};
    
    void *Inst = dios_create();    
    for (int n=0; n<nframes; n++) {
        fread(micbuf, sizeof(short), FRAMESIZE, fidmic);
        fread(farbuf, sizeof(short), FRAMESIZE, fidref);
        dios_process_frame(Inst, micbuf, farbuf, aecbuf);
        fwrite(aecbuf, sizeof(short), FRAMESIZE, fidaec);
    }
    dios_destroy(Inst);

    fclose(fidmic); fclose(fidref); fclose(fidaec);
    return 0;
}
#endif




/*老接口测试*/
// ./athena_signal_test -i mic00.pcm mic01.pcm mic02.pcm mic03.pcm -o aec45.pcm
#if 0
#include "dios_signal.h"

int main(int argc, char *argv[])
{
    /*aec test*/
    // int fe_switch[6] = {1/*aec*/,1/*ns*/,0/*agc*/,0/*hpf*/,0/*bf*/,0/*doa*/};
    // dios_ssp_v1(argc, argv, fe_switch, 0, NULL, 0, 1, 1, 0);

    int fe_switch[6] = {0/*aec*/,0/*ns*/,0/*agc*/,0/*hpf*/,1/*bf*/,0/*doa*/};
    int M = 4;
    float pos[4][3] = {{0.03f,0.0f,0.0f},{0.0f,0.03f,0.0f},
                            {-0.03f,0.0f,0.0f},{0.0f,-0.03f,0.0f}};
    

    dios_ssp_v1(argc, argv, fe_switch, 0, pos[0], M*3, M, 0, 270.0f);

    return 0;
}
#endif

/*
20ms转128点测试，20ms转256点测试
*/

#if 0
#include "ring_buffer.h"

int main(int argc, char *argv[])
{
    ring_buffer audioBuf = buffer_init(100*sizeof(short));
    short xbuf[5];
    short ybuf[5] = {0};

    for (int i=0; i<5; i++) {
        xbuf[i] = i;
    }
    size_t len = buffer_push(audioBuf, (void *)xbuf, 5*sizeof(short));
    printf("len = %ld\n", len);
    len = buffer_data_size(audioBuf); // 已存
    printf("len = %ld\n", len);
    len = buffer_space_available(audioBuf); // 剩下的
    printf("len = %ld\n", len);
    
    for (int i=0; i<5; i++) {
        xbuf[i] = i+5;
    }
    len = buffer_push(audioBuf, (void *)xbuf, 5*sizeof(short));
    printf("len = %ld\n", len);
    len = buffer_data_size(audioBuf); // 已存
    printf("len = %ld\n", len);
    len = buffer_space_available(audioBuf); // 剩下的
    printf("len = %ld\n", len);

    len = buffer_read(audioBuf, (void *)ybuf, 5*sizeof(short));
    for (int i=0; i<5; i++) printf("num = %d, value = %d\n", i, ybuf[i]);
    len = buffer_data_size(audioBuf); // 已存
    printf("len = %ld\n", len);
    len = buffer_space_available(audioBuf); // 剩下的
    printf("len = %ld\n", len);

    buffer_destroy(audioBuf);
    return 0;
}
#endif

